/*****************************************************
PROJECT: BRIDGE C2 SCRAP
JOB:
REQUESTOR:Rachel Gold
JIRA #:
ANALYST: Jean O'Malley
DATE: 8/4/2022
8/29/2022 rerun with corrected UTD at index
1/6/2023 added RIR_FOBT_prior, imaging_prior
VERSION: 
LAST UPDATED: 
PEER REVIEWER: 

PROJECT DESCRIPTION: Use ASCEND approach to compare CRC, CVC and 
BC cancer prevention services in patients with
	No SDH Screening
	Documented Need
	Documented No Need
Stratified by SDH need: Food, Housing, Transportation
STUDY PERIOD: 7/1/2016-2/29/2020		

DETAILS (this file): 
Table 1 for CRC
    Patients
	Race and ethnicity
	Preferred language
	Age at Index encounter (median, range)
	Age Group at Index
	Payer at Index
	FPL 
	Visit per Year
	Years of Observation
	CRC status at Index
	Any FIT/FOBT test prior to index
	Any Imaging screen prior to index
	Years of Observation after first Due date
	Ever due for screening during observation	
Columns Food Insecurity: Need/ No Need/ Not documented
Columns Housing Insecurity: Need/ No Need/ Not documented
Columns Transportation Insecurity: Need/ No Need/ Not documented
		

INPUT DATA:
SCRAP.CRC_patient_covariates;
SCRAP.CRC_status_at_index;

OUTPUT DATA:
SCRAP

RELATED FILE: 


SECTIONS:

****************************************************/
options compress=yes reuse=yes;
libname SCRAP "e:\\sasroot\BRIDGE C2\SCRAP\";
libname RD odbc noprompt="dsn=research_dev;Trusted_Connection=yes" bulkload=yes;
/****************************************************/

data analysis;
set SCRAP.CRC_patient_covariates;
run;
proc freq data=scrap.crc_analysis;
tables fit_fobt_prior*imaging_prior;
run;

/*Headers setting dynamic variables*/
%let cancer=CRC;


/*************************************************
STEP 1: 
Uncomment lines 71-73. comment out lines 101-115
Run full program to generate food_insecurity numbers
**************************************************/
%let sdh=SDH_food;
%let temp=scrap.temp_table1_food;
%let out_chi=scrap.temp_table1_chisq_food;
/*************************************************
STEP 2: 
comment out lines 71-73, Uncomment lines 81-83
Run full progarm to generate housing_insecurity numbers
**************************************************/
/*%let sdh=SDH_housing;*/
/*%let temp=scrap.temp_table1_housing;*/
/*%let out_chi=scrap.temp_table1_chisq_housing;*/

/*************************************************
STEP 3: 
comment out lines 81-83, Uncomment 90-92
Run full progarm to generate housing_insecurity numbers
**************************************************/
/*%let sdh=SDH_transportation;*/
/*%let temp=scrap.temp_table1_transportation;*/
/*%let out_chi=scrap.temp_table1_chisq_transportation;*/



/*************************************************
STEP 4: 
Uncomment and run lines 101-115
export  to excel
**************************************************/

data SCRAP.CRC_Table1;
merge scrap.temp_table1_food scrap.temp_table1_housing scrap.temp_table1_transportation;
by row variable;
run;
proc print data=scrap.temp_table1_chisq_food;
var characteristic prob;
run;
proc print data=scrap.temp_table1_chisq_housing;
var characteristic prob;
run;
proc print data=scrap.temp_table1_chisq_transportation;
var characteristic prob;
run;
proc print data=scrap.crc_table1;
run;


/*   Patients*/

proc freq data=analysis;
tables &sdh;
ods output onewayfreqs=sdh_cat;
run;

data Row1 (Keep=Row Variable total need_&sdh  
noneed_&sdh  nodoc_&sdh );
set sdh_cat;
row=1;
length Variable $25;
retain Variable total need_&sdh   
noneed_&sdh nodoc_&sdh;
variable='Patients_N';
total=.;

if &sdh='1_Positive Screen' then 
	need_&sdh=Frequency;
if &sdh='2_Negative Screen' then 
	noneed_&sdh=Frequency;
if &sdh='3_Never Screened' then do;
	total=CumFrequency;
	nodoc_&sdh=Frequency;
    output;
end;
run;

/*	sex*/
		
proc freq data=analysis;
tables sex_r*&sdh/nocol chisq;
ods output crosstabfreqs=sex_r chisq=chisq_sex&sdh;
run;
data Row3 (Keep=Row Variable total need_&sdh  
noneed_&sdh  nodoc_&sdh );
set sex_r;
where _type_ in('10','11') and sex_r='F';
row=3;
retain Variable total need_&sdh   
noneed_&sdh nodoc_&sdh;

length Variable $25;
Variable='Female, %';
if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
if _type_='10' then do;
		total=round(percent,0.1);
		output;
end;
run;
data &cancer;
set row1 row3;
run;
data chisq_sex&sdh;
set chisq_sex&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Sex';
run;
proc freq data=analysis;
tables race_ethnic*&sdh/nocol chisq;
ods output crosstabfreqs=race_ethnic chisq=chisq_race&sdh;
run;
data Row4_8 (Keep=Row Variable total need_&sdh  
noneed_&sdh  nodoc_&sdh );
set race_ethnic;
where _type_ in('10','11');
retain total need_&sdh noneed_&sdh nodoc_&sdh ;
total=.;
length Variable $25;
if race_ethnic='Hispanic' then do;
	Variable='Hispanic, %';
	Row=4;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if race_ethnic='Nonhispanic Black' then do;
	Variable='Non-Hispanic Black, %';
	Row=5;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;	
end;
if race_ethnic='Nonhispanic White' then do;
	Variable='Non-Hispanic White, %';
	Row=6;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;	
end;
if race_ethnic='Nonhispanic Other' then do;
	Variable='Non-Hispanic other, %';
	Row=7;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if race_ethnic='Unknown' then do;
	Variable='No data, %';
	Row=8;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
		end;
end;
run;
proc sort data=row4_8;
by row;
run;
data &cancer;
set &cancer row4_8;
run;
data chisq_race&sdh;
set chisq_race&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Race-ethnic';
run;

/*	Preferred language*/

proc freq data=analysis;
tables english*&sdh/nocol chisq ;
ods output crosstabfreqs=language chisq=chisq_language&sdh;
run;

data Row9_10 (Keep=Row Variable total need_&sdh  
noneed_&sdh  nodoc_&sdh );
set language;
where _type_ in('10','11');
retain row total need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
total=.;
if english=1 then do;
	Variable='English, %';
	Row=9;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if english=0 then do;
	Variable='Non-English, %';
	Row=10;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
run;
data &cancer;
set &cancer row9_10;
run;
data chisq_language&sdh;
set chisq_language&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Language';
run;
proc sort data=&cancer; by row;run;

/*	Age at Index encounter (median, range)*/

proc means median min max data=analysis;
var age_at_index_visit;
ods output summary=age1;
run;
proc means median min max data=analysis;
class &sdh;
var age_at_index_visit;
ods output summary=age2;
run;

data Row11A (Keep=Row variable total_txt);
set age1;
retain row need_&sdh.txt noneed_&sdh.txt nodoc_&sdh.txt ;
length Variable result $25;
median=put(age_at_index_visit_median, best2.);
min=put(age_at_index_visit_min, best2.);
max=put(age_at_index_visit_max, best2.);
comma=", ";
open=" (";
close=")";
total_txt=cat(median,open, min, comma, max, close);
Variable='Total';
	Row=11;
run;
data Row11B (Keep=Row variable need_&sdh.txt noneed_&sdh.txt nodoc_&sdh.txt);
set age2;
retain row need_&sdh.txt noneed_&sdh.txt nodoc_&sdh.txt ;
length Variable result $25;
median=put(age_at_index_visit_median, best2.);
min=put(age_at_index_visit_min, best2.);
max=put(age_at_index_visit_max, best2.);
comma=", ";
open=" (";
close=")";

result=cat(median,open, min, comma, max, close);
Variable='Age Median(Range)';
	Row=11;
	if &sdh='1_Positive Screen' then
		need_&sdh.txt=result;
	if &sdh='2_Negative Screen' then 
		noneed_&sdh.txt=result;
	if &sdh='3_Never Screened' then do;
		nodoc_&sdh.txt=result;
		output;
	end;
run;
data row11;
merge row11A row11b;
by row;
run;
data &cancer;
set &cancer row11;
run;

/*	Age Group at Index*/

proc freq data=analysis;
tables age_cat*&sdh/nocol chisq;
ods output crosstabfreqs=age_cat chisq=chisq_age&sdh;
run;


data Row12_13 (Keep=Row Variable total need_&sdh  
noneed_&sdh  nodoc_&sdh );;
set age_cat;
where _type_ in('10','11');
retain row total need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
if age_cat='50-64' then do;
	Variable='50-64';
	Row=12;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if age_cat='65-73' then do;
	Variable='65-73';
	Row=13;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
run;
data &cancer;
set &cancer row12_13;
run;
data chisq_age&sdh;
set chisq_age&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Age_cat';
run;

/*	Payer at Index*/

proc freq data=analysis;
tables payor_type_research*&sdh/nocol chisq;
ods output crosstabfreqs=payor chisq=chisq_payer&sdh;
run;


data Row14_18 (Keep=Row Variable total need_&sdh  
noneed_&sdh  nodoc_&sdh );;
set payor;
where _type_ in('10','11');
retain row total need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
if payor_type_research='Medicaid' then do;
	Variable='Medicaid';
	Row=14;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if payor_type_research='Medicare' then do;
	Variable='Medicare';
	Row=15;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if payor_type_research='Other Public' then do;
	Variable='Other public';
	Row=16;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if payor_type_research='Private' then do;
	Variable='Private';
	Row=17;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if payor_type_research='Uninsured' then do;
	Variable='Uninsured';
	Row=18;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
run;
data &cancer;
set &cancer row14_18;
run;

data chisq_payer&sdh;
set chisq_payer&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Payer';
run;

/*	FPL */

proc freq data=analysis;
tables FPL_cat*&sdh/nocol chisq ;
ods output crosstabfreqs=fpl chisq=chisq_FPL&SDH;
run;


data Row19_22 (Keep=Row Variable total need_&sdh  
noneed_&sdh  nodoc_&sdh );;
set fpl;
where _type_ in('10','11');
retain row total need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
if FPL_cat='<=100' then do;
	Variable='<=100';
	Row=19;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if FPL_cat='<=200' then do;
	Variable='>100 - 200';
	Row=20;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if FPL_cat='>200' then do;
	Variable='>200';
	Row=21;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if FPL_cat='No data' then do;
	Variable='No data';
	Row=22;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;

run;
data &cancer;
set &cancer row19_22;
run;
data chisq_FPL&sdh;
set chisq_FPL&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='FPL';
run;
/*	Visit per Year*/


proc freq data=analysis;
tables visit_cat*&sdh/nocol chisq;
ods output crosstabfreqs=visits chisq=chisq_visits&sdh;
run;

proc means min max data=scrap.crc_analysis;
class visit_cat;
var visits_first_yr yearly_visits;
run;*visit cat is based on visits/year of observation;



data Row23_26 (Keep=Row Variable total need_&sdh  
noneed_&sdh  nodoc_&sdh );;
set visits;
where _type_ in ('10','11');
retain row total need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
if visit_cat='1-2' then do;
	Variable='1-2 visits';
	Row=23;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if visit_cat='3-4' then do;
	Variable='3-4 visits';
	Row=24;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if visit_cat='5-6' then do;
	Variable='5-6 visits';
	Row=25;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if visit_cat='7+' then do;
	Variable='7+ visits';
	Row=26;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
run;
data &cancer;
set &cancer row23_26;
run;
data chisq_Visits&sdh;
set chisq_Visits&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Visits per Year';
run;


/*	Years of Observation*/

proc freq data=analysis;
tables obs_cat*&sdh/nocol chisq;
ods output crosstabfreqs=obs chisq=chisq_obs&sdh;
run;


data Row27_28 (Keep=Row total Variable need_&sdh  
noneed_&sdh  nodoc_&sdh );;
set obs;
where _type_ in('10','11');
retain row total need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
if obs_cat='<2' then do;
	Variable='<2 years';
	Row=27;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if obs_cat='2-3.7' then do;
	Variable='2-3.7 years';
	Row=28;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
run;
data &cancer;
set &cancer row27_28;
run;

data chisq_obs&sdh;
set chisq_obs&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Years of observation';
run;


/*	CRC status at Index*/
proc freq data=SCRAP.CRC_status_at_index;
tables status_at_index*&sdh/nocol chisq;
ods output crosstabfreqs=status chisq=chisqstatus&sdh;
run;

data Row29_30 (Keep=Row total Variable need_&sdh  
noneed_&sdh  nodoc_&sdh );;
set status;
where _type_ in('10','11');
retain row total need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
if status_at_index='Due' then do;
	Variable='Due';
	Row=29;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
if status_at_index='Screened Prior' then do;
	Variable='UTD at Index';
	Row=30;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;

run;
data &cancer;
set &cancer row29_30;
run;
data chisq_status&sdh;
set chisqstatus&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Status at Index';
run;
/*	FIT/FOBT test prior to Index: not necessarily up to date*/

proc freq data=SCRAP.CRC_analysis;
tables FIT_FOBT_prior*&sdh/nocol chisq;
ods output crosstabfreqs=FITFOBT chisq=chisq_FITFOBT&sdh;
run;





data Row31_32 (Keep=Row total Variable need_&sdh  
noneed_&sdh  nodoc_&sdh );;
set FITFOBT;
where _type_ in('10','11');
retain row total need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
if FIT_FOBT_prior=0 then do;
	Variable='No FIT/FOBT test prior to Index';
	Row=31;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
length Variable $25;
if FIT_FOBT_prior=1 then do;
	Variable='FIT/FOBT prior to Index';
	Row=32;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
run;

data &cancer;
set &cancer row31_32;
run;


/*Imaging screen priot to Index*/

proc freq data=SCRAP.CRC_analysis;
tables imaging_prior*&sdh/nocol chisq;
ods output crosstabfreqs=imaging chisq=chisq_imaging&sdh;
run;

data Row33_34 (Keep=Row total Variable need_&sdh  
noneed_&sdh  nodoc_&sdh );;
set imaging;
where _type_ in('10','11');
retain row total need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
if imaging_prior=0 then do;
	Variable='No imaging test prior to Index';
	Row=33;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
length Variable $25;
if imaging_prior=1 then do;
	Variable='Imaging prior prior to Index';
	Row=34;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
run;

data &cancer;
set &cancer row33_34;
run;

data chisq_imaging&sdh;
set chisq_Imaging&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Imaging prior to Index';
run;
/*	Ever due for screening during observation	*/
proc freq data=SCRAP.CRC_status_at_index;
tables ever_due*&sdh/nocol chisq;
ods output crosstabfreqs=ever_due chisq=chisq_neverdue&sdh;
run;
data Row35_36 (Keep=Row total Variable need_&sdh  
noneed_&sdh  nodoc_&sdh );;
set ever_due;
where _type_ in('10','11');
retain row total  need_&sdh noneed_&sdh nodoc_&sdh ;
length Variable $25;
if ever_due=0 then do;
	Variable='Not due during observation';
	Row=35;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
length Variable $25;
if ever_due=1 then do;
	Variable='Due during observation';
	Row=36;
	if &sdh='1_Positive Screen' then 
		need_&sdh=round(rowpercent,0.1);
	if &sdh='2_Negative Screen' then 
		noneed_&sdh=round(rowpercent,0.1);
	if &sdh='3_Never Screened' then 
		nodoc_&sdh=round(rowpercent,0.1);
	if _type_='10' then do;
		total=round(percent,0.1);
			output;
	end;
end;
run;
data &temp;
set &cancer row35_36;
run;
data chisq_neverdue&sdh;
set chisq_neverdue&sdh;
where statistic='Chi-Square';
length characteristic $15;
characteristic='Never due during obs';
run;
proc sort data=&temp;
by row variable total;
run;
data &out_chi;
set chisq_sex&sdh
chisq_race&sdh
chisq_language&sdh
chisq_age&sdh
chisq_payer&sdh
chisq_FPL&sdh
chisq_visits&sdh
chisq_obs&sdh
chisq_status&sdh
chisq_FITFOBT&SDH
chisq_imaging&sdh
chisq_neverdue&sdh;
run;
